package com.china08.yxyapi.util;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultSchool;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultTea;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultSchoolRepository;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultTeaRepository;

@Component
public class LectureUtil2 {

    private static final Logger LOGGER = LoggerFactory.getLogger(LectureUtil2.class);

    @Autowired
    private MongoTemplate mt;

    @Autowired
    private YxyHuodongLectureResultSchoolRepository schoolRepository;

    @Autowired
    private YxyHuodongLectureResultTeaRepository teaRepository;

    // 印台
    public void YtActivity(String schoolId, String userId, String userName, String schoolName) {

        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 11, 15);
        // 结束
        // 不包含当天 比如结束时间是:2017-11-25 此处写: 2017-11-16
        LocalDate end = LocalDate.of(2018, 1, 1);

        Date date2 = Date.valueOf(end);

        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("610000");
            lectureResultTea.setAreaNameProvince("陕西省");
            lectureResultTea.setAreaIdCity("610200");
            lectureResultTea.setAreaNameCity("铜川市");
            lectureResultTea.setAreaIdDistrict("610203");
            lectureResultTea.setAreaNameDistrict("印台区");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("610000");
            resultSchool.setAreaNameProvince("陕西省");
            resultSchool.setAreaIdCity("610200");
            resultSchool.setAreaNameCity("铜川市");
            resultSchool.setAreaIdDistrict("610203");
            resultSchool.setAreaNameDistrict("印台区");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }

    // 金沙
    public void JsActivity(String schoolId, String userId, String userName, String schoolName) {
        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 11, 22);
        // 结束
        LocalDate end = LocalDate.of(2018, 1, 1);
        Date date2 = Date.valueOf(end);
        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("520000");
            lectureResultTea.setAreaNameProvince("贵州省");
            lectureResultTea.setAreaIdCity("520500");
            lectureResultTea.setAreaNameCity("毕节市");
            lectureResultTea.setAreaIdDistrict("520523");
            lectureResultTea.setAreaNameDistrict("金沙县");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("520000");
            resultSchool.setAreaNameProvince("贵州省");
            resultSchool.setAreaIdCity("520500");
            resultSchool.setAreaNameCity("毕节市");
            resultSchool.setAreaIdDistrict("520523");
            resultSchool.setAreaNameDistrict("金沙县");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }
}
